<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客正文</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_list.css">

    <script src="js/jquery.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>

    <script src="js/urlutils.js"></script>
    <script src="js/logout.js"></script>
    <style>
        .user-info {
            margin-bottom: 20px;
        }

        .avatar {
            margin-bottom: 20px;
        }

        .avatar img {
            width: 150px;
            height: 150px;
            border-radius: 10%;
        }

        .user-details table {
            width: 100%;
        }

        .user-details td {
            padding: 5px;
        }

        .user-details .label {
            font-weight: bold;
        }

        .user-details .edit-btn {
            background-color: #007bff;
            color: #fff;
            border: none;
            padding: 5px 10px;
            border-radius: 5px;
            cursor: pointer;
        }

        .change-password table {
            width: 80%;
        }

        .change-password td {
            padding: 5px;
        }

        .change-password label {
            font-weight: bold;
        }

        .change-password input[type="password"] {
            width: 80%;
            padding: 10px;
        }

        .change-password .submit-btn {
            background-color: #28a745;
            color: #fff;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 20px 0px;
        }
        .styled-input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
        }

        .styled-input:focus {
            border-color: #007bff; /* 改变焦点时的边框颜色 */
            outline: none; /* 移除默认的焦点样式 */
        }

        .edit-btn:hover {
            background-color: #0056b3; /* 鼠标悬停时的背景颜色 */
        }
        .submit-btn:hover {
            background-color: #259644;
        }
    </style>
</head>
<body>
<div class="nav">
    <div class="flag">
        <img src="image/pen.svg" alt="logo">
        <div class="title">宝克书写笔销售系统</div>
    </div>

    <div class="page">
        <a href="seller_list.html">商品列表</a>
        <a href="shopping_cart.html">购物车</a>
        <a href="order_list.html">订单管理</a>
        <a href="user_info.html">个人中心</a>
        <a href="javascript:logout()">注销</a>
    </div>
</div>

<div class="containerX">
    <div class="rightX">
        <div class="user-info">
            <h3>用户中心</h3>
            <br>
            <div class="avatar">
                <img src="image/logo.png" alt="User Avatar" id="img">
            </div>
            <div class="user-details">
                <table>
                    <tr>
                        <td><span class="label">昵称</span></td>
                        <td><input type="text" class="styled-input" id="username"></td>
                        <td><button class="edit-btn" onclick="modefyUserName()">修改</button></td>
                    </tr>
                    <tr>
                        <td><span class="label">邮箱地址</span></td>
                        <td><input type="email" class="styled-input" id="email"></td>
                        <td><button class="edit-btn" onclick="modefyEmail()">修改</button></td>
                    </tr>
                    <tr>
                        <td><span class="label">电话号码</span></td>
                        <td><input type="text" class="styled-input" id="phone"></td>
                        <td><button class="edit-btn" onclick="modefyPhone()">修改</button></td>
                    </tr>
                    <tr>
                        <td><span class="label">收件地址</span></td>
                        <td><input type="text" class="styled-input" id="address"></td>
                        <td><button class="edit-btn" onclick="modefyAddress()">修改</button></td>
                    </tr>
                </table>
            </div>
        </div>
        <hr>
        <div class="change-password">
            <br>
            <h3 align="center">修改密码</h3>
            <br>
                <table>
                    <tr>
                        <td><label for="oldPassword">原密码</label></td>
                        <td><input type="password" id="oldPassword" name="oldPassword" class="styled-input" required></td>
                    </tr>
                    <tr>
                        <td><label for="newPassword">新密码</label></td>
                        <td><input type="password" id="newPassword" name="newPassword" class="styled-input" required></td>
                    </tr>
                    <tr>
                        <td><label for="confirmPassword">确认密码</label></td>
                        <td><input type="password" id="confirmPassword" name="confirmPassword" class="styled-input" required></td>
                    </tr>
                </table>
                <div style="text-align: center">
                    <button type="submit" class="submit-btn" onclick="modefyPwd()">提交修改</button>
                </div>
        </div>

    </div>
</div>
<script type="text/javascript">
    function modefyPwd() {
        // 获取表单数据
        var oldPassword = $("#oldPassword").val().trim();
        var newPassword = $("#newPassword").val().trim();
        var confirmPassword = $("#confirmPassword").val().trim();

        // 前端验证新密码和确认密码是否相同
        if (newPassword !== confirmPassword) {
            alert("新密码和确认密码不匹配，请重新输入。");
            return;
        }

        // 发送 AJAX 请求
        $.ajax({
            url: "/user/modefyPwd",
            type: "POST",
            data: {
                oldPassword: oldPassword, // 原密码
                newPassword: newPassword,  // 新密码
                passwordRepeat: confirmPassword // 确认密码
            },
            success: function(res) {
                if (res.code == 200) {
                    // 修改成功跳转到登录页
                    alert("修改密码成功！")
                    location.href = "login.html";
                } else {
                    alert("修改失败:"+res.meg);
                }
            }
        });
    }

    function modefyUserName() {
        // 2.向后端请求数据
        jQuery.ajax({
            url:"/user/modefyInfo",
            type:"POST",
            data:{
                "username":$("#username").val().trim()
            },
            success:function(res) {
                // 3.将数据展示给用户
                if (res.code == 200) {
                    alert("修改成功！")
                } else {
                    alert("修改失败:"+res.meg);
                }
            }
        });
    }
    function modefyEmail() {
        // 2.向后端请求数据
        jQuery.ajax({
            url:"/user/modefyInfo",
            type:"POST",
            data:{
                "email":$("#email").val().trim()
            },
            success:function(res) {
                // 3.将数据展示给用户
                if (res.code == 200) {
                    alert("修改成功！")
                } else {
                    alert("修改失败:"+res.meg);
                }
            }
        });
    }
    function modefyPhone() {
        // 2.向后端请求数据
        jQuery.ajax({
            url:"/user/modefyInfo",
            type:"POST",
            data:{
                "phone":$("#phone").val().trim()
            },
            success:function(res) {
                // 3.将数据展示给用户
                if (res.code == 200) {
                    alert("修改成功！")
                } else {
                    alert("修改失败:"+res.meg);
                }
            }
        });
    }
    function modefyAddress() {
        // 2.向后端请求数据
        jQuery.ajax({
            url:"/user/modefyInfo",
            type:"POST",
            data:{
                "address":$("#address").val().trim()
            },
            success:function(res) {
                // 3.将数据展示给用户
                if (res.code == 200) {
                    alert("修改成功！")
                } else {
                    alert("修改失败:"+res.meg);
                }
            }
        });
    }
    // 初始化页面
    function init() {
        // 2.向后端请求数据
        jQuery.ajax({
            url:"/user/info",
            type:"GET",
            success:function(res) {
                // 3.将数据展示给用户
                if (res.code == 200) {
                    var art = res.data;
                    if (art != null) {

                        $("#img").attr("src", art.photo);
                        $("#username").val(art.username);
                        $("#email").val(art.email);
                        $("#phone").val(art.phone);
                        $("#address").val(art.address);

                    } else {
                        alert("查询失败:"+res.meg);
                    }
                } else {
                    alert("查询失败:"+res.meg);
                }
            }
        });
    }
    init();


</script>
</body>
</html>